package com.cxb;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.cxb.receiver.LockReceiver;
import com.cxb.utils.Debug;
  
public class LockScreenActivity extends BaseActivity {

	private DevicePolicyManager policyManager = null; 
    private ComponentName componentName = null; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
			componentName = new ComponentName(this, LockReceiver.class);
			if (policyManager.isAdminActive(componentName)) {// 判断是否有权限(激活了设备管理器)
				policyManager.lockNow();// 直接锁屏
			} else { // 激活设备管理器获取权限 ,改了Settings的DeviceAdminAdd,直接添加,不弹出对话框
				Intent intent = new Intent(
						DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
				intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
						componentName);
				intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
						"LockScreen");
				startActivityForResult(intent, 0);
				if (policyManager.isAdminActive(componentName)) {
					policyManager.lockNow();
				}
			}
		} catch (Exception e) {
			Debug.printf(e.toString(), null);
		}
		finish();
	}
}
